/**
 * 
 */
package com.alianzamedica.controllers.drug;

import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.objectsearch.sqlsearch.ObjectSearch;
import org.w3c.dom.Document;

import com.alianzamedica.businessobject.Drug;
import com.alianzamedica.businessobject.Produit;
import com.alianzamedica.tools.Enviroment;

/**
 * @author RMarquez_Inf
 * 
 */
public class EditDrugAction extends Action {
	@SuppressWarnings("unchecked")
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String sId = request.getParameter("id");
		Integer id = null;
		if (sId != null && sId.trim().length() > 0) {
			try {
				id = Integer.valueOf(sId);
			} catch (Exception e) {
			}
		}
		Enviroment env = Enviroment.getInstance();
		Document doc = env.getDocument();
		ObjectSearch search = new ObjectSearch(doc,
				"com.alianzamedica.connection.ConnectionImpl");
		if (id != null) {
			Drug t = new Drug();
			t.setId(id);
			Iterator<Drug> iterator = search.searchObjects(t).iterator();
			if (iterator.hasNext()) {
				Drug drug = iterator.next();
				request.setAttribute("drug", drug);
			}
		}
		
		Produit produit=new Produit();
		ArrayList produits = search.searchObjects(produit);
		request.setAttribute("produits", produits);
		
		return mapping.findForward("success");
	}
}
